<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.img.php');
$msgPag = reqVar('msgpag');
if (reqVar('azione') == 'salva') {
	$msgPag = confMod();
} 
if (reqVar('azione') == '') {
	$sql_sel = "SELECT d.*" .
			" FROM ovf_tabelle d " .
			" WHERE d.id_tabella = ".intval(reqVar('id'));
	$objDB->query($sql_sel,'selRecDom');
	$acRecDom = $objDB->fetch('selRecDom');
	$_REQUEST['cod_tabella'] = $acRecDom['cod_tabella'];
	$_REQUEST['descr'] = $acRecDom['descr'];
	$_REQUEST['lista_campi'] = $acRecDom['lista_campi'];
	$_REQUEST['nota'] = $acRecDom['nota'];
}
$aTipi = array("T"=>"Tabella","R"=>"Regola");
testaHtml('Gestione tabelle', 'Modifica tabella',$msgPag, 'gest');
?>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
         &nbsp;&nbsp;&nbsp;
         <a class="cmd1" href="javascript:document.frm1.submit();" >salva</a>
			<? echo cmdTorna('torna'); ?>
      </td>
   </tr>
</table>
<form name='frm1' action='<?=$_SERVER['PHP_SELF']?>' method='post' enctype="multipart/form-data">
<input type='hidden' name='azione' value="salva">
<input type='hidden' name='id' value="<?=reqVar('id')?>">
<input type='hidden' name='xsn' value="<?=XSN?>">
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="tit1" colspan='2' align='center'>Tabella</td>
   </tr>
   <tr>
      <td class="lab1" width='150px'>Codice</td>
		<td class="txt1">
			<?=getHtmlTextcode('cod_tabella',16,16);?>
		</td>
   </tr>
   <tr>
      <td class="lab1">Descrizione</td>
		<td class="txt1">
			<?=getHtmlText('descr',80,80);?>
		</td>
   </tr>
   <tr>
      <td class="lab1">Nota</td>
		<td class="txt1">
			<?=getHtmlTextArea('nota',80,5);?>
		</td>
   </tr>
   <tr>
      <td class="lab1">Lista campi</td>
		<td class="txt1">
			<?=getHtmlTextArea('lista_campi',80,2);?>
		</td>
   </tr>
</table>
</form>
<?
codaHtml();
exit();
function confMod() {
	global $objDB;
	$msg = '';
	$cod_tabella = strtoupper(trim(reqVar('cod_tabella')));
	if (empty($cod_tabella)) {
		$msg .= "codice obbligatorio<br>";
	}
	if (preg_match("/[^-_A-Z0-9]/",$cod_tabella)) {
		$msg = "codice '$cod_tabella' puo' contenere solo lettere numeri '-' e '_'<br>";
	}
	$descr = trim(reqVar('descr'));
	if (empty($descr)) {
		$msg .= "descrizione obbligatorio<br>";
	}
	$lista_campi = trim(reqVar('lista_campi'));
	$sqlSel = "SELECT id_tabella FROM ovf_tabelle " .
			" WHERE cod_tabella = '".addslashes($cod_tabella)."'" .
					" AND id_tabella <> ".intval(reqVar('id'));
	$objDB->exec($sqlSel,'insIst');
	$aRow = $objDB->fetch('insIst');
	if ($aRow) {
		$msg .= "codice tabella gia' esistente<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	$sSqlIns = "UPDATE ovf_tabelle SET cod_tabella = '".addslashes($cod_tabella)."'" .
			", descr = '".addslashes($descr)."'".
			", nota = '".reqVarAS('nota')."'".
			", lista_campi = '".addslashes($lista_campi)."'" .
			" WHERE id_tabella = ".intval(reqVar('id'));
	$objDB->exec($sSqlIns);
	//return "<pre>".print_r($objDB,1)."</pre>";
	header("Location: tabelleView.php?id=".reqVar('id')."&xsn=".XSN);
	exit();
}
?>